package com.zfjt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00008;
import com.zfjt.service.BrabchService;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.service.UserService;


/**
 * 上传客户信息
 * @author xk
 * @version 1.0
 * @date 2017年3月31日
 */
@Controller
public class AddMessageController {

	private ReturnBean mReturn;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	@Autowired
	private UserService mUser;
	
	@Autowired
	private BrabchService mBrabch;
	
	private Map<String, String> fhdata;
	
	@RequestMapping(value="00008")
	@ResponseBody
	public ReturnBean AddMessage(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00008 num00008 = JSON.parseObject(data,PackNum00008.class); 	
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00008");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			//获取数据库类数据数量方便为新的客户命名ID
			String branch = mUser.selectByPrimaryKey(num00008.getAccount()).getBranch();
			String branchName = mBrabch.selectByPrimaryKey(branch).getBrabchName();
			List<ClientmessageBean> js = mClientMessage.selectAll();
			ClientmessageBean mData = new ClientmessageBean();
			if (js.size() == 0) {
				mData.setId((short) 1);
			}else {
				int size = js.size();
				int id = js.get( size - 1).getId();
				mData.setId((short) (id + 1));
			}
			mData.setName(num00008.getName());
			mData.setAddtime(num00008.getAddtime());
			mData.setMobile(num00008.getMobile());
			mData.setPlatenumbers(num00008.getPlateNumbers());
			mData.setReject("0");
			mData.setType("1");
			mData.setIsaccomplish("0");
			mData.setCharge("0");
			mData.setRejectImg("0");
			mData.setProduct(num00008.getProduct());
			mData.setGenre(num00008.getGenre());
			mData.setAffiliationYewu(num00008.getAccount());
			mData.setMotorcycleType(num00008.getVehicle());
			mData.setAffiliationWangdian(branch);//把该订单上级网点ID存入
			mData.setText1(branchName);//存入网点名字
			mData.setAffiliationDaqu(mBrabch.selectByPrimaryKey(branch).getSubordinate());//把该订单上级业务经理存入
			mClientMessage.insert(mData);
			mReturn.setCode("000");
			mReturn.setPortnum("00008");
			fhdata.put("id", String.valueOf(js.size() + 1));
			mReturn.setData(JSON.toJSONString(fhdata));				
		}
		return mReturn;
	}
}
